// created by noellynch
// Jun 4, 2009

package com.sceneworker.editors.glslshadereditor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.concurrent.Callable;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import net.miginfocom.swing.MigLayout;

import com.jme.scene.state.GLSLShaderObjectsState;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.shader.ShaderVariable;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SimpleAlert;
import com.myjavautils.swing.SwingComponentFactory;
import com.sceneworker.app.globals.SceneWorkerDialogManager;
import com.sceneworker.editors.glslshadereditor.UniformRepresentations.IUniformRepresentation;
import com.sceneworker.editors.glslshadereditor.uniformreps.EditUniformPanel;

public class UniformsPanel extends JPanel {

	private		GLSLShaderObjectsState		mcl_shaderState;
	private		JList						mcl_uniformsList;
	private		JTextArea					mcl_value;
	
	public	UniformsPanel(GLSLShaderObjectsState cl_shaderState) {
		mcl_shaderState = cl_shaderState;
		
		setLayout(new MigLayout("inset 5", "[grow, fill]"));
		
		JLabel		lcl_uniformsLabel = SwingComponentFactory.makeALabel("Uniforms");
		add(lcl_uniformsLabel, "wrap");
		
		initialiseUniformsList();
		
		initialiseValueTextArea();
		
		initialiseButtons();
	}
	
	private	void	initialiseButtons() {
		final	UniformsPanel		lcl_this = this;
		
		JButton		mcl_new = SwingComponentFactory.makeAButton("New");
		mcl_new.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JDialog		lcl_dlg = SceneWorkerDialogManager.inst().makeADialog(SceneWorkerDialogManager.NEW_UNIFORM_VARIABLE_DIALOG);
				NewUniformPanel		lcl_uniformPanel = new NewUniformPanel(mcl_shaderState, lcl_this, lcl_dlg);
				lcl_dlg.add(lcl_uniformPanel);
				lcl_dlg.pack();
				lcl_dlg.setVisible(true);
			}
		});
		JButton		mcl_edit = SwingComponentFactory.makeAButton("Edit");
		mcl_edit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ShaderVariable		lcl_sel = mcl_shaderState.getUniformByName((String)mcl_uniformsList.getSelectedValue());
				
				if(lcl_sel == null) {
					SimpleAlert.doAlert("You have not selected a uniform to edit.", null);
					return;
				}
				
				JDialog		lcl_dlg = SceneWorkerDialogManager.inst().makeADialog(SceneWorkerDialogManager.EDIT_UNIFORM_DIALOG);
				lcl_dlg.add(new EditUniformPanel(mcl_shaderState, lcl_sel, lcl_dlg, lcl_this));
				lcl_dlg.pack();
				lcl_dlg.setVisible(true);
			}
		});
		
		MigLayoutUtils.makeRightAlignedButtonBar(this, mcl_new, mcl_edit);
	}
	
	public	void	updateUniformValueField() {
		ShaderVariable		lcl_sel = mcl_shaderState.getUniformByName((String)mcl_uniformsList.getSelectedValue());
		
		if(lcl_sel == null) {
			mcl_value.setText("");
			return;
		}
		
		IUniformRepresentation		lcl_rep = UniformRepresentations.getRepresentationByClass(lcl_sel.getClass());
		if(lcl_rep == null) {
			mcl_value.setText("");
			return;
		}
		
		mcl_value.setText(lcl_rep.getValueRep(lcl_sel));
		mcl_value.repaint();
	}
	
	public	void	updateUniformsList() {
		DefaultListModel		lcl_model = new DefaultListModel();
		
		Collection<ShaderVariable>		lcla_uniforms = mcl_shaderState.getShaderUniforms();
		
		for(ShaderVariable lcl_shaderVar : lcla_uniforms) {
			lcl_model.addElement(lcl_shaderVar.name);
		}
		
		mcl_uniformsList.setModel(lcl_model);
	}
	
	private	void	initialiseUniformsList() {
		DefaultListModel		lcl_model = new DefaultListModel();
		
		Collection<ShaderVariable>		lcla_uniforms = mcl_shaderState.getShaderUniforms();
		
		for(ShaderVariable lcl_shaderVar : lcla_uniforms) {
			lcl_model.addElement(lcl_shaderVar.name);
		}
		
		mcl_uniformsList = new JList();
		mcl_uniformsList.setModel(lcl_model);
		mcl_uniformsList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				GameTaskQueueManager.getManager().update(new Callable<Void>() {
					public Void call() throws Exception {
						updateUniformValueField();
						return null;
					}
				});
			}
		});
		
		
		JScrollPane		lcl_scroll = new JScrollPane();
		lcl_scroll.setViewportView(mcl_uniformsList);
		add(lcl_scroll, "w 100%, h 50%, wrap");
	}
	
	private	void	initialiseValueTextArea() {
		JLabel		lcl_uniformsLabel = SwingComponentFactory.makeALabel("Value");
		add(lcl_uniformsLabel, "wrap");
		
		mcl_value = new JTextArea();
		mcl_value.setTabSize(2);
		mcl_value.setLineWrap(true);
		mcl_value.setWrapStyleWord(true);
		JScrollPane		lcl_scroll = new JScrollPane();
		
		lcl_scroll.setViewportView(mcl_value);
		add(lcl_scroll, "w 100%, h 40%, wrap");
	}
}
